package main

import (
	"github.com/siddontang/go-mysql/client"
	"github.com/siddontang/go-mysql/mysql"
	"github.com/siddontang/go-mysql/server"
	"log"
	"net"
)

type MyHandler struct {
	server.EmptyHandler
	conn *client.Conn
}

func NewMyHandler() *MyHandler {
	conn, err := client.Connect("127.0.0.1:33306", "root", "123456", "test")
	if err != nil {
		log.Fatal(err)
	}
	return &MyHandler{conn: conn}
}

func (handler MyHandler) HandleQuery(query string) (*mysql.Result, error) {
	return handler.conn.Execute(query)
}

func main() {

	l, _ := net.Listen("tcp", "127.0.0.1:4000")

	for {

		c, _ := l.Accept()

		go func() {

			conn, _ := server.NewConn(c, "root", "", NewMyHandler())

			for {
				conn.HandleCommand()
			}
		}()
	}
}
